package com.atguigu.mock.entity;

import com.atguigu.common.base.entity.AutoIdBase;
import com.atguigu.common.comment.Comment;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@NoArgsConstructor
@Comment("订单明细表")
public class OrderDetail extends AutoIdBase {
    @Comment("下单用户")
    @ManyToOne(optional = false)
    private Customer customer;

    @Comment("下单店铺")
    @ManyToOne(optional = false)
    private Shop shop;

    @Comment("订单ID")
    @ManyToOne(optional = false)
    private OrderInfo orderInfo;

    @Comment("菜品ID")
    @ManyToOne
    private ProductSku productSku;

    @Comment("套餐ID")
    @ManyToOne
    private ProductGroup productGroup;

    @Comment("数量")
    private Integer skuNum;

    @Comment("金额")
    private BigDecimal amount;

    public OrderDetail(LocalDateTime createTime, OrderInfo orderInfo, ProductSku productSku, ProductGroup productGroup, Integer skuNum) {
        super(createTime);
        this.customer = orderInfo.getCustomer();
        this.shop = orderInfo.getShop();
        this.orderInfo = orderInfo;
        this.skuNum = skuNum;
        if (productSku != null) {
            this.productSku = productSku;
            this.productGroup = null;
            this.amount = productSku.getPrice().multiply(new BigDecimal(skuNum));
        } else {
            this.productGroup = productGroup;
            this.productSku = null;
            this.amount = productGroup.getPrice().multiply(new BigDecimal(skuNum));
        }
    }
}
